package com.qiye.livevideo.utils;

import com.qiye.livevideo.common.RetConstant;
import com.qiye.livevideo.model.AliCloudLiveInfo;
import com.qiye.livevideo.service.AliCloudLiveInfoService;
import com.qiye.livevideo.service.AliCloudMessageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created on 2019/4/29.
 *获取拉流地址demo
 * @author meishijun
 */
@Component
public class AliLivePullFlowUtils {
    /**
     * 固定值，无需改变
     */
    private static final String FLOW_FORMAT_RTMP = "rtmp";
    /**
     * 固定值，无需改变
     */
    private static final String FLOW_FORMAT_FLV = "flv";
    @Autowired
    private AliCloudLiveInfoService aliCloudLiveInfoService;
    private static AliLivePullFlowUtils aliLivePullFlowUtils;

    @PostConstruct
    public void init() {
        aliLivePullFlowUtils = this;
        aliLivePullFlowUtils.aliCloudLiveInfoService = this.aliCloudLiveInfoService;
    }

    public static  AliCloudLiveInfo getLiveInfoByAccessKeyId(String accessKeyId){
        if(accessKeyId==null || accessKeyId.isEmpty()){
            accessKeyId = RetConstant.DEFAULT_ACCESS_KEY_ID;
        }
        AliCloudLiveInfo aliCloudLiveInfo = aliLivePullFlowUtils.aliCloudLiveInfoService.selectByaccessKeyId(accessKeyId);
        if(aliCloudLiveInfo!=null){
            return  aliCloudLiveInfo;
        }
        return new AliCloudLiveInfo();
    }

    /**
     * 获取拉流地址
     * @param format
     * @param appName
     * @param streamName
     * @param endTime
     * @return
     */
    public static String getPullUrl(String format,String appName, String streamName,Long endTime,String accessKeyId) {
        if(format!=null){
            AliCloudLiveInfo aliCloudLiveInfo = getLiveInfoByAccessKeyId(accessKeyId);
            if (FLOW_FORMAT_RTMP.equals(format)) {
                return "rtmp://"+ aliCloudLiveInfo.getPullDomainName() +getRoomName(appName,streamName,format) + generateAuthKey(format,appName,streamName,endTime,aliCloudLiveInfo.getPullDomainAuthKey());
            }else{
                return "http://"+ aliCloudLiveInfo.getPullDomainName() +getRoomName(appName,streamName,format) + generateAuthKey(format,appName,streamName,endTime,aliCloudLiveInfo.getPullDomainAuthKey());
            }
        }else {
            return "要生成的播流地址格式不能为空!";
        }

    }

    /**
     * 根据格式判断
     * @param appName
     * @param streamName
     * @param format
     * @return
     */
    private static String getRoomName(String appName,String streamName,String format) {
        if(FLOW_FORMAT_RTMP.equals(format)){
            return "/"+appName + "/"+streamName;
        }else if(FLOW_FORMAT_FLV.equals(format)){
            return "/"+appName + "/"+streamName+".flv";
        }else{
            return "/"+appName + "/"+streamName+".m3u8";
        }
    }

    /**
     * 完整验签串
     * @param format
     * @param appName
     * @param streamName
     * @param endTime
     * @return
     */
    private static String generateAuthKey(String format,String appName,String streamName,Long endTime,String pullDomainAuthKey) {
        return "?auth_key=" + endTime + generateUuid() + generateEncryptStr(format,appName,streamName,endTime,pullDomainAuthKey);
    }

    /**
     * 唯一标识
     * @return
     */
    private static String generateUuid() {
        String uuid = "0";
        String uid = "0";
        return "-" + uuid + "-" + uid + "-";
    }

    /**
     * 验签密钥
     * @param format
     * @param appName
     * @param streamName
     * @param endTime
     * @return
     */
    private static String generateEncryptStr(String format,String appName,String streamName,Long endTime,String pullDomainAuthKey) {
        String md5push = getRoomName(appName,streamName,format)+"-"+endTime+"-0-0-"+pullDomainAuthKey;
        return Md5Utils.getMd5Code(md5push);
    }

}
